<template>
  <div>
    <el-card style="margin: 20px 0px">
      <categourySelect
        @getcategoryId="getcategoryId"
        :show="scene != 0"
      ></categourySelect>
    </el-card>

    <el-card>
      <div v-show="scene == 0">
        <!-- 展示spu属性列表的结构 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!category3Id"
          @click="addSpu"
          >添加SPU</el-button
        >
        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="spuName" label="spu名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="spu描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row }">
              <el-button
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                @click="UpdateSpu(row)"
              ></el-button>
              <el-button
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看当前spu所有信息"
                @click="handeler(row)"
              ></el-button>
              <el-popconfirm
                style="marginleft: 10px"
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                :title="`你是否删除${row.spuName}`"
                @onConfirm="deleteList(row)"
              >
                <el-button
                  slot="reference"
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 
           @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
         -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          style="textalign: center"
          :current-page="page"
          :page-sizes="[3, 5, 7]"
          :page-size="limit"
          layout=" prev, pager, next, jumper,->,total, sizes"
          :total="total"
        >
        </el-pagination>
      </div>
      <SpuForm
        v-show="scene == 1"
        @changescene="changeScene"
        ref="spu"
      ></SpuForm>
      <SkuForm
        v-show="scene == 2"
        ref="sku"
        @changescenes="changeScene"
      ></SkuForm>
    </el-card>
    <el-dialog
      :title="`${spu.spuName}的sku列表`"
      :visible.sync="dialogTableVisible"
      :before-close="close"
    >
      <el-table :data="skuList" style="width: 100%" border v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="width">
        </el-table-column>
         <el-table-column prop="price" label="价格" width="width">
        </el-table-column>
         <el-table-column prop="weight" label="重量" width="width">
        </el-table-column>
         <el-table-column prop="" label="默认图片" width="width">
          <template slot-scope="{row}">
           <img :src="row.skuDefaultImg" style="width:100px;height:100px" >
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SkuForm from "./SkuForm";
import SpuForm from "./SpuForm";
export default {
  name: "Spu",
  data() {
    return {
      //控制对话框的显示与隐藏
      dialogTableVisible: false,

      category1Id: "",
      category2Id: "",
      category3Id: "",
      show: true,
      //当前的页数
      page: 1,
      //每一页展示多少条数据
      limit: 3,
      records: [],
      total: 0,
      //控制显示与隐藏
      scene: 0,
      spu: {},
      skuList: [],
      loading:true
    };
  },
  methods: {
    getcategoryId({ categoryId, level }) {
      if (level == 1) {
        this.category1Id = categoryId;
        //清除2id和3id
        this.category2Id = "";
        this.category3Id = "";
      } else if (level == 2) {
        this.category2Id = categoryId;
        this.category3Id = "";
      } else {
        this.category3Id = categoryId;
        //调用方法
        this.getSpuList();
      }
    },
    async getSpuList() {
      const { page, limit, category3Id } = this;
      const reslut = await this.$API.spu.reqSkuList(page, limit, category3Id);
      if (reslut.code == 200) {
        this.total = reslut.data.total;
        this.records = reslut.data.records;
      }
    },
    handleCurrentChange(page) {
      this.page = page;
      this.getSpuList();
    },
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSpuList();
    },
    addSpu() {
      this.scene = 1;
      this.$refs.spu.addGetDate(this.category3Id);
    },
    UpdateSpu(row) {
      this.scene = 1;
      this.$refs.spu.inintSpuData(row);
    },
    changeScene(scene) {
      this.scene = scene;
      this.getSpuList();
    },
    async deleteList(row) {
      let reslut = await this.$API.spu.reqDeleteList(row.id);
      if (reslut.code == 200) {
        this.$message({
          type: "success",
          message: "删除成功",
        });
        this.getSpuList();
      }
    },
    addSku(row) {
      this.scene = 2;
      this.$refs.sku.getDate(row, this.category1Id, this.category2Id);
    },
    async handeler(spu) {
      
      this.dialogTableVisible = true;
      this.spu = spu;

      //发请求
      let reslut = await this.$API.spu.reqSkuListNone(spu.id);
      if (reslut.code == 200) {
        this.skuList = reslut.data;
        this.loading = false
      }
    },
    close(done) {
      this.loading = true
      this.skuList = []
      done()
    }
  },
  components: {
    SkuForm,
    SpuForm,
  },
};
</script>

<style>

</style>
